import { _decorator, color, director, System } from 'cc';
import { subgame002 } from './UIRoot';
import { gm } from '../../scripts/managers/GameManager';
import { GameKey } from '../../scripts/managers/SubgameConfig';
import { SubgameManager } from '../../scripts/managers/SubgameManager';
import { FlowStatus } from './Enums';
import { ReactiveProperty } from '../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { TweenToStart } from '../../scripts/libs/rxjs/cc3/TweenStart';
import { EMPTY, Observable, map } from 'rxjs';
import { ResType, rm } from '../../scripts/managers/ResourceManager';
import { uim } from '../../scripts/libs/ui/UIManager';
import { ReactiveCommand } from '../../scripts/libs/rxjs/cc3/ReactiveCommand';
import { EventTouch } from 'cc';
import { Card } from './ui/MainPanel/Card';
const { ccclass, property } = _decorator;

@ccclass('Game002Manager')
export class Game002Manager extends SubgameManager {

    public static instance: Game002Manager;
    static ID = 'game002_system';
    get logColorOverride() { return color().fromHEX('#EECCDD') };

    static registerSystem() {
        director.unregisterSystem(director.getSystem(Game002Manager.ID));
        director.registerSystem(Game002Manager.ID, game002, System.Priority.MEDIUM);
        game002.init();
    }

    static unregisterSystem() {
        game002.destroy();
        director.unregisterSystem(director.getSystem(Game002Manager.ID));
    }

    gameKey = GameKey.subgame002;

    onInitManager() {
        this.log('onInitManager', this.timestamp);
        gm.subgameManagers[this.gameKey] = this;
    }

    onDestroyManager(): void {
        this.log('onDestroyManager', this.timestamp);
        delete gm.subgameManagers[this.gameKey];
    }

    uiRoot: subgame002.UIRoot = null;

    flowStatus = ReactiveProperty.Create(FlowStatus.Empty);
    loadingEnabled = ReactiveProperty.Create(true);

    useObserves() {
        return [
            rm.finishedCount.subscribe(_ => { gm.curProgressValue.value = _ / this.totalResCount * gm.maxProgressValue }),
        ];
    }

    totalResCount = 12;

    resList = ([
        ['ab_subgame002/Prefabs/MainPanel/MainPanel', ResType.Prefab],
        ['ab_subgame002/texture-0/poker', ResType.SpriteAtlas]
    ]);

    onEnterGame(): Observable<any> {
        this.log('@enterGame');
        return new Observable(observer => {
            rm.loadRes(this.resList).subscribe({
                next: _ => { this.log('loadAssets next', _); },
                complete: () => {
                    this.log('实际资源数量:', rm.finishedCount.value, '预期资源数量:', this.totalResCount);
                    this.flowStatus.value = FlowStatus.Main;
                    this.loadingEnabled.value = false;
                    observer.next();
                    observer.complete();
                }
            });
        });
    }

    onLeaveGame() {
        this.log('@leaveGame');
        rm.finishedCount.value = 0;
        this.flowStatus.value = FlowStatus.Empty;
        this.loadingEnabled.value = true;
        return uim.standby.whenTrueOnce().pipe(map(_ => {
            rm.releaseRes(this.resList);
        }));
    }

    cardNodeTouchCommand = ReactiveCommand.Create<{ card: Card, eventTouch: EventTouch }>();
}

globalThis.Game002Manager = Game002Manager;

export const game002 = Game002Manager.instance = new Game002Manager();